package dk.ihk.shopular.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import dk.ihk.shopular.models.ShoppingItem;
import dk.ihk.shopular.models.ShoppingList;
import dk.ihk.shopular.models.User;
import dk.ihk.shopular.services.ShoppingService;
import dk.ihk.shopular.services.UserService;

@Controller
public class ShoppingController {
	@Autowired
	private ShoppingService shoppingService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/lists/my")
	public String displayMyLists(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		List<ShoppingList> lists = shoppingService.getUsersShoppingLists(request);
		request.setAttribute("lists", lists);
		
		return "listLists";
	}

	@RequestMapping("/lists/newList")
	public String showListDialog(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		List<User> friends = userService.getCurrentUser(request).getFriends();
		request.setAttribute("friends", friends);
		
		return "newList";
	}
	
	@RequestMapping("/lists/detail")
	public String showShoppingList(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		String listName = request.getParameter("name");
		ShoppingList list = shoppingService.getShoppingListByName(listName);
		
		//get users that share this list
		
		//get all friends which are not in the above list (those which are "addable")
		
		
		request.setAttribute("list", list);
		
		//TODO: make sure user can only access his own lists!!
		return "listDetail";
	}
	
	@RequestMapping("/lists/createList")
	public String createNewList(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		User currentUser = userService.getCurrentUser(request);
		//TODO get selected friends
		
		ShoppingList newList = new ShoppingList();
		newList.setName(request.getParameter("name"));
		
		shoppingService.addToShoppingListPool(newList);
		currentUser.addShoppingList(newList);
		
		String[] friends = request.getParameterValues("friends");
		for (String friend : friends) {
			userService.findUserByName(friend).addShoppingList(newList);
		}
		
		request.setAttribute("lists", currentUser.getShoppingLists());
		return "listLists";
	}
	
	@RequestMapping("/list/addItem")
	public String addItemToList(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		String itemName = request.getParameter("itemName");
		String listName = request.getParameter("list");
		
		ShoppingList list = shoppingService.getShoppingListByName(listName);
		ShoppingItem item = new ShoppingItem();
		item.setName(itemName);
		list.addItem(item);
		
		//redirect
		String redirectUrl = "/shopular/lists/detail.do?name=" + listName;
		try {
			response.sendRedirect(redirectUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "listLists";
	}
	
	//some getters and setters
	public void setShoppingService(ShoppingService shoppingService) {
		this.shoppingService = shoppingService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
